#!/usr/bin/perl -w
#   Written by Jimmy Saw - 02/24/11
#   This program takes a Genbank file and extract coordinates of genes 
#   You have to redirect output to a file

use Bio::SeqIO;

my $seqio_obj = Bio::SeqIO->new (-file => $ARGV[0], -format => 'genbank');
my $seq_obj = $seqio_obj->next_seq;

for my $feat_obj ($seq_obj->get_SeqFeatures){
  if ($feat_obj->primary_tag eq "CDS"){
    if ($feat_obj->has_tag('locus_tag')){
      for my $value ($feat_obj->get_tag_values('locus_tag')){
        my $start = $feat_obj->start;
        my $end = $feat_obj->end;
        my $strand = "";
        if ($feat_obj->strand eq "1"){
          $strand = "+";
        }
        else{
          $strand = "-";
        }
        #my $strand = $feat_obj->strand;
        print "CDS", "\t", $value, "\t", $start, "\t", $end, "\t", $strand, "\t";
      }
    }
    if ($feat_obj->has_tag('gene')){
      for my $value ($feat_obj->get_tag_values('gene')){
        print $value, "\t";
      }
    }
    else{
      print "-", "\t";
    }
    if ($feat_obj->has_tag('protein_id')){
      for my $value ($feat_obj->get_tag_values('protein_id')){
        print $value, "\t";
      }
    }
    else{
      print "-", "\t";
    }

    if ($feat_obj->has_tag('EC_number')){
      for my $value ($feat_obj->get_tag_values('EC_number')){
        print $value, "\t";
      }
    }
    else{
      print "-", "\t";
    }

    if ($feat_obj->has_tag('db_xref')){
      for my $value ($feat_obj->get_tag_values('db_xref')){
        print $value, "\t";
      }
    }
    else{
      print "-", "\t";
    }

    if ($feat_obj->has_tag('note')){
      for my $value ($feat_obj->get_tag_values('note')){
        print $value, "\t";
      }
    }
    else{
      print "-", "\t";
    }

    if ($feat_obj->has_tag('product')){
      for my $value ($feat_obj->get_tag_values('product')){
        print $value, "\n";
      }
    }
    else{
      print "-", "\t";
    }
  }
  if ($feat_obj->primary_tag eq "rRNA"){
    if ($feat_obj->has_tag('locus_tag')){
      for my $locus_tag ($feat_obj->get_tag_values('locus_tag')){
        my $start = $feat_obj->start;
        my $stop = $feat_obj->end;
        my $strand = "";
        if ($feat_obj->strand eq "1"){
          $strand = "+";
        }
        else{
          $strand = "-";
        }
        for my $tag ($feat_obj->get_all_tags) {
          for my $value ($feat_obj->get_tag_values($tag)) {
             if ($tag eq "product"){
               print "rRNA", "\t", $locus_tag,"\t", $start, "\t", $stop, "\t", $strand, "\t", $value, "\n";
             }
          }
        }
      }
    }
  }
  if ($feat_obj->primary_tag eq "tRNA"){
    if ($feat_obj->has_tag('locus_tag')){
      for my $locus_tag ($feat_obj->get_tag_values('locus_tag')){
        my $start = $feat_obj->start;
        my $stop = $feat_obj->end;
        my $strand = "";
        if ($feat_obj->strand eq "1"){
          $strand = "+";
        }
        else{
          $strand = "-";
        }
        for my $tag ($feat_obj->get_all_tags) {
          for my $value ($feat_obj->get_tag_values($tag)) {
             if ($tag eq "product"){
               print "tRNA", "\t", $locus_tag,"\t", $start, "\t", $stop, "\t", $strand, "\t", $value, "\n";
             }
          }
        }
      }
    }
  }
  if ($feat_obj->primary_tag eq "misc_feature"){
    my $start = $feat_obj->start;
    my $stop = $feat_obj->end;
    my $strand = "";
    if ($feat_obj->strand eq "1"){
      $strand = "+";
    }
    else{
      $strand = "-";
    }
    for my $tag ($feat_obj->get_all_tags) {             
      for my $value ($feat_obj->get_tag_values($tag)) {        
        print "misc_feature", "\t", $start, "\t", $stop, "\t", $strand, "\t", $value, "\n";    
      }
    }
  }
}
